package com.example.farmeri.ui.farm_manager;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;

import com.example.farmeri.R;
import com.example.farmeri.data.DataParam;
import com.example.farmeri.data.RetrofitManager;
import com.example.farmeri.http.API;
import com.example.farmeri.tools.ResponseUtils;
import com.example.farmeri.ui.base.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import okhttp3.FormBody;
import okhttp3.RequestBody;

public class FarmManagerAddActivity extends BaseActivity {

    Switch switchTemperature;

    Switch switchHumidity;

    Switch switchSun;

    Switch switchMessage;

    EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        setContentView(R.layout.activity_farm_manager_add);
        init();
    }

    private void init() {
        switchTemperature=findViewById(R.id.switch_temperature1);
        switchHumidity=findViewById(R.id.switch_humidity1);
        switchSun=findViewById(R.id.switch_sun1);
        switchMessage=findViewById(R.id.switch_message1);
        editText = findViewById(R.id.add_manager_edit);
        ((Button)findViewById(R.id.add_manager_button)).setOnClickListener((v)->{
            submit();
        });
    }

    public void submit(){
        String ident = getIdent();
        RequestBody body = new FormBody.Builder()
                .add("farmer_id", DataParam.getUserInfo().getId())
                .add("ident",ident)
                .add("managerName",editText.getText().toString())
                .build();
        RetrofitManager.getManager().getRetrofit()
                .create(API.class)
                .addFarmManager(body)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseUtils<String>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull ResponseUtils<String> responseUtils) {
                        if (responseUtils.getCode() == 200){
                            showToast("添加成功");
                            finish();
                        }else {
                            showToast(responseUtils.getMessage());
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        showToast("添加失败");
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

    private String getIdent() {
        StringBuilder sb = new StringBuilder();
        if (switchTemperature.isChecked()){
            sb.append(",温度管理员");
        }
        if (switchHumidity.isChecked()){
            sb.append(",湿度管理员");
        }
        if (switchSun.isChecked()){
            sb.append(",光照管理员");
        }
        if (switchMessage.isChecked()){
            sb.append(",消息管理员");
        }
        return sb.length()>0?
                sb.substring(1)
                :sb.toString();
    }
}